package com.htl.exampro.base.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * JWT拦截器
 */
@Component
public class JwtInterceptor implements HandlerInterceptor {

//    @Autowired
//    private JwtUtil jwtUtil;
//
//    @Autowired
//    private StringRedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        // 新增登录路径直接放行逻辑
//        String requestURI = request.getRequestURI();
//        if ("/user/user/login".equals(requestURI)||"/user/user/register".equals(requestURI)) {
//            return true;
//        }
//        // 从请求头中获取token
//        String token = request.getHeader("Authorization");
//        if (StringUtils.hasLength(token)
//                && token.equals(redisTemplate.opsForValue().get(token))) {
//            redisTemplate.expire(token, jwtUtil.getExpiration(), TimeUnit.MINUTES);
//            // 将用户信息存入request
//            Claims claims = jwtUtil.getClaimsFromToken(token);
//            request.setAttribute("userId", claims.get("userId"));
//            request.setAttribute("username", claims.get("username"));
//            request.setAttribute("userType", claims.get("userType"));
//            UsernamePasswordAuthenticationToken authenticationToken =
//                    new UsernamePasswordAuthenticationToken(claims,null,null);
//            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//            return true;
//        }
//        response.setStatus(401);
//        return false;
        return true; //jwt登录验证通过filter实现
    }

}